package com.agileboot.attendance.domain.order.db.impl;

import com.agileboot.attendance.domain.order.db.AttOrderPersonnelMapper;
import com.agileboot.attendance.domain.order.db.AttOrderPersonnelService;
import com.agileboot.attendance.domain.order.dto.OrderPersonnelDTO;
import com.agileboot.attendance.domain.order.dto.OrderPersonnelQuery;
import com.agileboot.attendance.domain.order.entity.AttOrderPersonnelEntity;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 订单人员关联服务实现类
 *
 * @author agileboot
 */
@Service
@RequiredArgsConstructor
public class AttOrderPersonnelServiceImpl extends ServiceImpl<AttOrderPersonnelMapper, AttOrderPersonnelEntity> implements AttOrderPersonnelService {
    
    /**
     * 获取订单关联人员列表
     *
     * @param orderId 订单ID
     * @return 人员列表
     */
    @Override
    public List<OrderPersonnelDTO> getOrderPersonnelList(Long orderId) {
        return baseMapper.selectOrderPersonnelList(orderId);
    }
    
    /**
     * 分页查询订单关联人员列表
     *
     * @param query 查询参数
     * @return 分页结果
     */
    @Override
    public Page<OrderPersonnelDTO> getOrderPersonnelPage(OrderPersonnelQuery query) {
        Page<OrderPersonnelDTO> page = new Page<>(query.getPageNum(), query.getPageSize());
        return baseMapper.selectOrderPersonnelPage(
                page,
                query.getOrderId(),
                query.getPersonnelName(),
                query.getPersonnelCode(),
                query.getStatus());
    }
    
    /**
     * 检查订单人员关联是否已存在
     *
     * @param orderId 订单ID
     * @param personnelId 人员ID
     * @return 是否存在
     */
    @Override
    public boolean checkOrderPersonnelExists(Long orderId, Long personnelId) {
        LambdaQueryWrapper<AttOrderPersonnelEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AttOrderPersonnelEntity::getOrderId, orderId)
                .eq(AttOrderPersonnelEntity::getPersonnelId, personnelId)
                .eq(AttOrderPersonnelEntity::getDeleted, 0);
        return this.count(queryWrapper) > 0;
    }
} 